package com.edu.dgut.controller;

import com.edu.dgut.dao.administratorsRepository;
import com.edu.dgut.entity.administratorsEntity;
import com.edu.dgut.service.mapObjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/")
@PreAuthorize("hasRole('ROLE_ADMIN')")
public class administratorsController {

    @Autowired
    private administratorsRepository administrators;

    private mapObjectService mapObject = new mapObjectService();

    //查询所有管理员
    @RequestMapping("getAlladministrators")
    @ResponseBody
    public Map<String, Object> findAll(){
        List<administratorsEntity> list = new ArrayList<>();
        list = administrators.findAll();
        return mapObject.put(list);
    }

    //通过姓名查找管理员
    @RequestMapping("getByName")
    @ResponseBody
    public Map<String, Object> getByName(String name) {
        List<administratorsEntity> list = administrators.findByNameLike("%"+name+"%");
        return mapObject.put(list);
    }

    //添加或修改管理员信息
    @RequestMapping("modifyAdministrators")
    @ResponseBody
    public Map<String, Object> saveOrModifyAdministrator(@RequestBody administratorsEntity administrator) {
        //若输入新的管理员信息，则将数据库最大工号加1存为最新管理员的工号，并将密码加密后保存
        if(administrator.getAccount()==0){
            int account = administrators.findAccount();
            int new_account = account + 1;
            administrator.setAccount(new_account);
            //将输入的新密码加密
            String gensalt = BCrypt.gensalt();//获取盐
            String saltPassword = BCrypt.hashpw(administrator.getPassword(), gensalt);//加盐
            administrator.setPassword(saltPassword);
        }
        administrator = administrators.save(administrator);
        Map<String,Object> map = new HashMap<String,Object>();
        if(administrator != null) {
            map.put("status", 1);
        }else {
            map.put("status", 0);
        }
        return map;
    }

    //修改管理员状态
    @RequestMapping("modifyAdministratorsState")
    @ResponseBody
    public Map<String, Object> ModifyAdministratorState(@RequestBody administratorsEntity administrator) {
        administratorsEntity new_administrator = new administratorsEntity();
        new_administrator = administrators.findByAccount(administrator.getAccount());
        new_administrator.setState(administrator.getState());
        new_administrator = administrators.save(new_administrator);
        Map<String,Object> map = new HashMap<String,Object>();
        if(new_administrator != null) {
            map.put("status", 1);
        }else {
            map.put("status", 0);
        }
        return map;
    }

    //修改密码
    @RequestMapping("modifyPassword")
    @ResponseBody
    public Map<String, Object> modifyPassword(String old_password, String new_password) {
        Map<String, Object> map = new HashMap<String, Object>();
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = userDetails.getUsername();
        administratorsEntity old_administrator = new administratorsEntity();
        administratorsEntity new_administrator = new administratorsEntity();
        old_administrator = administrators.findByName(name);
        //验证输入的旧密码是否正确
        boolean check = BCrypt.checkpw(old_password, old_administrator.getPassword());
        if(check) {
            //将输入的新密码加密
            String gensalt = BCrypt.gensalt();//获取盐
            String saltPassword = BCrypt.hashpw(new_password, gensalt);//加盐
            old_administrator.setPassword(saltPassword);
            new_administrator = administrators.save(old_administrator);
            if (new_administrator != null) {
                map.put("status", 1);
            } else {
                map.put("status", 0);
            }
            return map;
        }else{
            map.put("status", 0);
            return map;
        }
    }

    //通过工号删除管理员
    @RequestMapping("deleteByAccount")
    @ResponseBody
    public Map<String, Object> deleteByName(int account) {
        int i = administrators.deleteByAccount(account);
        Map<String,Object> map = new HashMap<String,Object>();
        if(i>=1) {
            map.put("status", 1);
        }else {
            map.put("status", 0);
        }
        return map;
    }

}
